"use strict";TABS.dataflash={available:!1},TABS.dataflash.initialize=function(a){function e(){$("#content").load("./tabs/dataflash.html",function(){l()})}function t(a){if(a<1024)return a+"B";var e=a/1024;if(e<1024)return Math.round(e)+"kB";var t=e/1024;return t.toFixed(1)+"MB"}function s(){DATAFLASH.usedSize>0?($(".tab-dataflash .dataflash-used").css({width:DATAFLASH.usedSize/DATAFLASH.totalSize*100+"%",display:"block"}),$(".tab-dataflash .dataflash-used div").text("Used space "+t(DATAFLASH.usedSize))):$(".tab-dataflash .dataflash-used").css({display:"none"}),DATAFLASH.totalSize-DATAFLASH.usedSize>0?($(".tab-dataflash .dataflash-free").css({width:(DATAFLASH.totalSize-DATAFLASH.usedSize)/DATAFLASH.totalSize*100+"%",display:"block"}),$(".tab-dataflash .dataflash-free div").text("Free space "+t(DATAFLASH.totalSize-DATAFLASH.usedSize))):$(".tab-dataflash .dataflash-free").css({display:"none"}),$(".tab-dataflash a.erase-flash, .tab-dataflash a.save-flash").toggleClass("disabled",0==DATAFLASH.usedSize)}function l(){if(translateAll(),TABS.dataflash.available){var e=DATAFLASH.totalSize>0;$(".tab-dataflash").toggleClass("supported",e),e?($(".tab-dataflash a.erase-flash").click(h),$(".tab-dataflash a.erase-flash-confirm").click(u),$(".tab-dataflash a.erase-flash-cancel").click(S),$(".tab-dataflash a.save-flash").click(r),$(".tab-dataflash a.save-flash-cancel").click(i),$(".tab-dataflash a.save-flash-dismiss").click(d),s()):$(".tab-dataflash .note").html(getMessage("dataflashNotSupportedNote"))}else $(".tab-dataflash").removeClass("supported"),$(".tab-dataflash .note").html(getMessage("dataflashFirmwareUpgradeRequired"));a&&a()}function n(a,e){for(a=""+a;a.length<e;)a="0"+a;return a}function i(){g=!0}function o(){$(".dataflash-saving progress").attr("value",0),g=!1,$(".dataflash-saving").removeClass("done"),$(".dataflash-saving")[0].showModal()}function d(){$(".dataflash-saving")[0].close()}function f(){$(".dataflash-saving").addClass("done")}function r(){var a=DATAFLASH.usedSize;GUI.connected_to&&c(function(e){function t(l,n){if(null!=n)if(n.byteLength>0){s+=n.byteLength,$(".dataflash-saving progress").attr("value",s/a*100);var i=new Blob([n]);e.onwriteend=function(e){g||s>=a?g?d():f():MSP.dataflashRead(s,t)},e.write(i)}else f();else MSP.dataflashRead(s,t)}var s=0;o(),MSP.dataflashRead(s,t)})}function c(a){var e=new Date,t="blackbox_log_"+e.getFullYear()+"-"+n(e.getMonth()+1,2)+"-"+n(e.getDate(),2)+"_"+n(e.getHours(),2)+n(e.getMinutes(),2)+n(e.getSeconds(),2);chrome.fileSystem.chooseEntry({type:"saveFile",suggestedName:t,accepts:[{extensions:["TXT"]}]},function(e){var t=chrome.runtime.lastError;return t?(console.error(t.message),void("User cancelled"!=t.message&&GUI.log(getMessage("dataflashFileWriteFailed")))):(chrome.fileSystem.getDisplayPath(e,function(a){console.log("Dataflash dump file path: "+a)}),void e.createWriter(function(e){e.onerror=function(a){console.error(a)},a(e)},function(a){console.error(a),GUI.log(getMessage("dataflashFileWriteFailed"))}))})}function h(){v=!1,$(".dataflash-confirm-erase").removeClass("erasing"),$(".dataflash-confirm-erase")[0].showModal()}function A(){MSP.send_message(MSP_codes.MSP_DATAFLASH_SUMMARY,!1,!1,function(){s(),v||(DATAFLASH.ready?$(".dataflash-confirm-erase")[0].close():setTimeout(A,500))})}function u(){$(".dataflash-confirm-erase").addClass("erasing"),MSP.send_message(MSP_codes.MSP_DATAFLASH_ERASE,!1,!1,A)}function S(){v=!0,$(".dataflash-confirm-erase")[0].close()}var g,v;"dataflash"!=GUI.active_tab&&(GUI.active_tab="dataflash",googleAnalytics.sendAppView("dataflash"));if(CONFIGURATOR.connectionValid){if(TABS.dataflash.available=CONFIG.apiVersion>=1.6,!TABS.dataflash.available)return void e();MSP.send_message(MSP_codes.MSP_DATAFLASH_SUMMARY,!1,!1,e)}},TABS.dataflash.cleanup=function(a){a&&a()};